﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UofCASE.AgileProductLinerDSL;
using Microsoft.VisualStudio.Modeling.Diagrams;
using System.Drawing;

namespace APLDTests {
    [TestClass]
    public class ConfigColoringHandlerTest {
        
        [TestMethod]
        public void TestConfigColoring() {
            ModelHelper model = ModelHelper.GetModel(true);
            
            //get all instances we need for this test
            Feature feature = model.GetElement<Feature>("feature");
            Feature feature2 = model.GetElement<Feature>("feature2");
            Feature feature3 = model.GetElement<Feature>("feature3");
            FeatureShape fShape = model.GetElement<FeatureShape>("featureShape");
            FeatureShape fShape2 = model.GetElement<FeatureShape>("featureShape2");
            FeatureShape fShape3 = model.GetElement<FeatureShape>("featureShape3");
            FeatureShape fShape4 = model.GetElement<FeatureShape>("featureShape4");
            TestShape testShape = model.GetElement<TestShape>("testShape");
            TestShape testShape2 = model.GetElement<TestShape>("testShape2");
            TestShape testShape3 = model.GetElement<TestShape>("testShape3");
            TestPortShape tPShape = model.GetElement<TestPortShape>("testPortShape");
            TestPortShape tPShape2 = model.GetElement<TestPortShape>("testPortShape2");
            TestPortShape tPShape3 = model.GetElement<TestPortShape>("testPortShape3");
            TestConnector testCon = model.GetElement<TestConnector>("testCon");
            TestConnector testCon2 = model.GetElement<TestConnector>("testCon2");
            TestConnector testCon3 = model.GetElement<TestConnector>("testCon3");
            TestConnector testCon4 = model.GetElement<TestConnector>("testCon4");
            ConnectConnector featCon = model.GetElement<ConnectConnector>("featCon");
            ConnectConnector featCon2 = model.GetElement<ConnectConnector>("featCon2");
            ConnectConnector featCon3 = model.GetElement<ConnectConnector>("featCon3");

            //map one test, so it color will be cornflower blue instead of silver
            CommandHandler.MapTest(testShape3, @"Project\Folder\Test.html", @"Project\Project.csproj");

            //create the list of features which should be in the config
            IList<Feature> checkedFeatures = new List<Feature>();
            checkedFeatures.Add(feature);
            checkedFeatures.Add(feature2);

            //the shapebackground of a feature shape, a testport shape, the test connector line and the connect connector line
            //will not be overwritten unless they're grayed out (the color will be determined by the classstyle if not)
            //therefor we check the brushes itself
            Assert.AreEqual(Color.LightBlue, (fShape.StyleSet.GetBrush(DiagramBrushes.ShapeBackground) as SolidBrush).Color);
            Assert.IsNull(fShape.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground));
            Assert.AreEqual(Color.LightBlue, (fShape2.StyleSet.GetBrush(DiagramBrushes.ShapeBackground) as SolidBrush).Color);
            Assert.IsNull(fShape2.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground));
            Assert.AreEqual(Color.LightBlue, (fShape3.StyleSet.GetBrush(DiagramBrushes.ShapeBackground) as SolidBrush).Color);
            Assert.IsNull(fShape3.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground));
            Assert.AreEqual(Color.LightBlue, (fShape4.StyleSet.GetBrush(DiagramBrushes.ShapeBackground) as SolidBrush).Color);
            Assert.IsNull(fShape4.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground));

            Assert.AreEqual(Color.Gray, testCon.StyleSet.GetPen(DiagramPens.ConnectionLine).Color);
            Assert.IsNull(testCon.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine));
            Assert.AreEqual(Color.Gray, testCon2.StyleSet.GetPen(DiagramPens.ConnectionLine).Color);
            Assert.IsNull(testCon2.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine));
            Assert.AreEqual(Color.Gray, testCon3.StyleSet.GetPen(DiagramPens.ConnectionLine).Color);
            Assert.IsNull(testCon3.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine));
            Assert.AreEqual(Color.Gray, testCon4.StyleSet.GetPen(DiagramPens.ConnectionLine).Color);
            Assert.IsNull(testCon4.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine));

            Assert.AreEqual(Color.Black, featCon.StyleSet.GetPen(DiagramPens.ConnectionLine).Color);
            Assert.IsNull(featCon.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine));
            Assert.AreEqual(Color.Black, featCon2.StyleSet.GetPen(DiagramPens.ConnectionLine).Color);
            Assert.IsNull(featCon2.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine));
            Assert.AreEqual(Color.Black, featCon3.StyleSet.GetPen(DiagramPens.ConnectionLine).Color);
            Assert.IsNull(featCon3.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine));

            Assert.AreEqual(Color.Orange, tPShape.StyleSet.GetPen(DiagramPens.ShapeOutline).Color);
            Assert.IsNull(testCon.StyleSet.GetOverriddenPenSettings(DiagramPens.ShapeOutline));
            Assert.AreEqual(Color.Orange, tPShape2.StyleSet.GetPen(DiagramPens.ShapeOutline).Color);
            Assert.IsNull(tPShape2.StyleSet.GetOverriddenPenSettings(DiagramPens.ShapeOutline));
            Assert.AreEqual(Color.Orange, tPShape3.StyleSet.GetPen(DiagramPens.ShapeOutline).Color);
            Assert.IsNull(tPShape3.StyleSet.GetOverriddenPenSettings(DiagramPens.ShapeOutline));


            //the shapebackgound color of a testshape will always be overwritten, as the color will be set according to mapping and test results
            Assert.AreEqual(Color.Silver, testShape.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground).Color);
            Assert.AreEqual(Color.Silver, testShape2.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground).Color);
            //test 3 is mappeed, so it's cornflower blue
            Assert.AreEqual(Color.CornflowerBlue, testShape3.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground).Color);

            //update the coloring according to the checkedFeatures list
            ConfigColoringHandler.UpdateDiagramSelection(model.Store, checkedFeatures);

            Assert.AreEqual(Color.LightBlue, (fShape.StyleSet.GetBrush(DiagramBrushes.ShapeBackground) as SolidBrush).Color);
            Assert.IsNull(fShape.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground));
            Assert.AreEqual(Color.LightBlue, (fShape2.StyleSet.GetBrush(DiagramBrushes.ShapeBackground) as SolidBrush).Color);
            Assert.IsNull(fShape2.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground));
            Assert.AreEqual(Color.Gainsboro, fShape3.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground).Color);
            Assert.AreEqual(Color.Gainsboro, fShape4.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground).Color);

            Assert.AreEqual(Color.Silver, testShape.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground).Color);
            Assert.AreEqual(Color.Silver, testShape2.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground).Color);
            Assert.AreEqual(Color.Gainsboro, testShape3.StyleSet.GetOverriddenBrushSettings(DiagramBrushes.ShapeBackground).Color);

            Assert.AreEqual(Color.Black, featCon.StyleSet.GetPen(DiagramPens.ConnectionLine).Color);
            Assert.IsNull(featCon.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine));
            
            //TODO Check model helper for correct building of ConnectConnectors
            //Assert.AreEqual(Color.Gainsboro, featCon2.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine).Color);
            //Assert.AreEqual(Color.Gainsboro, featCon3.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine).Color);

            Assert.AreEqual(Color.Gray, testCon.StyleSet.GetPen(DiagramPens.ConnectionLine).Color);
            Assert.IsNull(testCon.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine));
            Assert.AreEqual(Color.Gray, testCon2.StyleSet.GetPen(DiagramPens.ConnectionLine).Color);
            Assert.IsNull(testCon2.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine));
            Assert.AreEqual(Color.Gainsboro, testCon3.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine).Color);
            Assert.AreEqual(Color.Gainsboro, testCon4.StyleSet.GetOverriddenPenSettings(DiagramPens.ConnectionLine).Color);

            Assert.AreEqual(Color.Orange, tPShape.StyleSet.GetPen(DiagramPens.ShapeOutline).Color);
            Assert.IsNull(testCon.StyleSet.GetOverriddenPenSettings(DiagramPens.ShapeOutline));
            Assert.AreEqual(Color.LightGray, tPShape2.StyleSet.GetOverriddenPenSettings(DiagramPens.ShapeOutline).Color);
            Assert.AreEqual(Color.LightGray, tPShape3.StyleSet.GetOverriddenPenSettings(DiagramPens.ShapeOutline).Color);
        }
    }
}
